/*
 * Copyright (C) 2012 Le Tuan Anh <anh.takaji@gmail.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package ezcms.ejb;

import ezcms.dal.AbstractDAOFactory;
import ezcms.dal.DAOFactory;
import ezcms.dal.ReviewDAO;
import ezcms.dto.Assessment;
import ezcms.dto.User;
import ezcms.exceptions.BusinessLogicException;
import ezcms.exceptions.DataLayerException;
import java.util.Iterator;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.ejb.Stateless;

/**
 *
 * @author Le Tuan Anh <anh.takaji@gmail.com>
 */
@Stateless(mappedName = "ezcms.ReviewService")
public class ReviewServiceFacade implements ReviewServiceRemote {
    
    private DAOFactory factory;

    protected DAOFactory getFactory() {
        if (factory == null) {
            factory = AbstractDAOFactory.getDAOFactory(AbstractDAOFactory.HIBERNATE_MYSQL);
        }
        return factory;
    }
    
    @EJB
    private UserManagerBean userManagerBean;

    @Override
    public void assignReviewers(List<User> userList) {
        //the parameter may need tht submission info to see which submission to review
        
        Iterator<User> i = userList.iterator();
        while(i.hasNext()){
           User user = i.next();
            try {
                String email = userManagerBean.getUser(user.getFirstName()).getEmail();
                //send email to reviewer
            } catch (BusinessLogicException ex) {
                Logger.getLogger(ReviewServiceFacade.class.getName()).log(Level.SEVERE, null, ex);
            }
           
        }
    }

    @Override
    public void createReview(Assessment assessment){
        ReviewDAO dao = getFactory().getReviewDAO();
        try {
            dao.createReview(assessment);
        } catch (DataLayerException ex) {
            Logger.getLogger(ReviewServiceFacade.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    @Override
    public Assessment findReview(Assessment assessment){
        ReviewDAO dao = getFactory().getReviewDAO();
        Assessment as = new Assessment();
        try {            
            as = dao.findReview(assessment);            
        } catch (DataLayerException ex) {
            Logger.getLogger(ReviewServiceFacade.class.getName()).log(Level.SEVERE, null, ex);
        }
        return as;
        
    }
    
    @Override
    public void updateReivew(Assessment assessment){
        ReviewDAO dao = getFactory().getReviewDAO();
        try {
            dao.updateReview(assessment);
        } catch (DataLayerException ex) {
            Logger.getLogger(ReviewServiceFacade.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    // Add business logic below. (Right-click in editor and choose
    // "Insert Code > Add Business Method")
    
}
